package com.alipay.bean;

import com.alipay.config.AlipayConfig;
import com.alipay.util.AlipaySubmit;
import com.alipay.util.UtilDate;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by cqd on 2016-01-11.
 * 提现请求bean封装
 */
public class Withdraw {
    public String service = "batch_trans_notify";//退款有密接口名称
    public String partner = AlipayConfig.partner;//合作者身份ID
    public String _input_charset = "utf-8";//参数编码字符集
    public String sign_type = AlipayConfig.sign_type;//签名方式
    public String notify_url = AlipayConfig.withdraw_notify_url;//通知回调页面
    public String account_name = AlipayConfig.accountName;//卖家支付宝账号
    public String batch_no = UtilDate.getDate() + System.currentTimeMillis();//提现批次号
    public String Email = AlipayConfig.paymentAccount;
    public String pay_date = UtilDate.getDate();
    public String detail_data;//付款数据集合
    public String sign;//签名
    public String batch_num;//总笔数
    public String batch_fee;//付款总金额

    public String getService() {
        return service;
    }

    public void setService(String service) {
        this.service = service;
    }

    public String getPartner() {
        return partner;
    }

    public void setPartner(String partner) {
        this.partner = partner;
    }

    public String get_input_charset() {
        return _input_charset;
    }

    public void set_input_charset(String _input_charset) {
        this._input_charset = _input_charset;
    }

    public String getSign_type() {
        return sign_type;
    }

    public void setSign_type(String sign_type) {
        this.sign_type = sign_type;
    }

    public String getNotify_url() {
        return notify_url;
    }

    public void setNotify_url(String notify_url) {
        this.notify_url = notify_url;
    }

    public String getAccount_name() {
        return account_name;
    }

    public void setAccount_name(String account_name) {
        this.account_name = account_name;
    }

    public String getBatch_no() {
        return batch_no;
    }

    public void setBatch_no(String batch_no) {
        this.batch_no = batch_no;
    }

    public String getEmail() {
        return Email;
    }

    public void setEmail(String email) {
        Email = email;
    }

    public String getPay_date() {
        return pay_date;
    }

    public void setPay_date(String pay_date) {
        this.pay_date = pay_date;
    }

    public String getDetail_data() {
        return detail_data;
    }

    public void setDetail_data(String detail_data) {
        this.detail_data = detail_data;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public String getBatch_num() {
        return batch_num;
    }

    public void setBatch_num(String batch_num) {
        this.batch_num = batch_num;
    }

    public String getBatch_fee() {
        return batch_fee;
    }

    public void setBatch_fee(String batch_fee) {
        this.batch_fee = batch_fee;
    }

    /**
     * 生成付款数据集内容和总笔数和总金额
     *
     * @param list 付款数据集合
     * @return 支付宝批量支付界面url
     */
    public String builderDetailData(List<WithdrawDetailData> list) {
        if (list == null || list.size() == 0) {
            return null;
        }
        StringBuffer temp = new StringBuffer();
        BigDecimal totalFee = new BigDecimal(0.00);
        for (int i = 0; i < list.size(); i++) {
            WithdrawDetailData data = list.get(i);
            String trade_no = data.getNum();
            String money = data.getMoney();
            BigDecimal bigDecimal = new BigDecimal(money);
            totalFee=totalFee.add(bigDecimal);
            String desc = data.getDesc();
            String realName = data.getRealName();
            String account = data.getAccount();
            temp.append(trade_no);
            temp.append("^");
            temp.append(account);
            temp.append("^");
            temp.append(realName);
            temp.append("^");
            temp.append(money);
            temp.append("^");
            temp.append(desc);
            if (i != list.size() - 1) {//不是最后一个，需要添加#号区别开
                temp.append("|");
            }
        }
        this.detail_data = temp.toString();
        this.batch_num = String.valueOf(list.size());
        this.batch_fee = totalFee.toString();
        this.sign = AlipaySubmit.buildNoQuataRequest(toMap());
        return AlipayConfig.withdraw_url + this.sign;
    }

    //获取类各个字段数值，并生成map,主要用于签名
    public Map<String, String> toMap() {
        Map<String, String> map = new HashMap<String, String>();
        Field[] fields = this.getClass().getDeclaredFields();
        for (Field field : fields) {
            Object obj;
            try {
                obj = field.get(this);
                if (obj != null) {
                    if (!field.getName().equals("sign_type")) {//过滤掉加密类型
                        map.put(field.getName(), obj.toString());
                    }
                }
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return map;
    }

    public List<WithdrawDetailData> getTestData() {
        List<WithdrawDetailData> list = new ArrayList<WithdrawDetailData>();
        for (int i = 0; i < 1; i++) {
            WithdrawDetailData detailData = new WithdrawDetailData();
            detailData.setAccount("chen64418471@163.com");
            detailData.setDesc("测试" + i);
            detailData.setMoney("0.01");
            detailData.setNum(String.valueOf(System.currentTimeMillis()));
            detailData.setRealName("陈启斗");
            list.add(detailData);
        }
        return list;

    }

    public static void main(String[] args) {
        Withdraw withdraw = new Withdraw();
        withdraw.builderDetailData(withdraw.getTestData());
        System.out.println(AlipayConfig.withdraw_url + withdraw.sign);
        //  System.out.println(refund.sign_type);
    }
}
